package cn.java86.dao;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
@Service("userDao")
public class UserDaoImpl implements UserDao {
    private static List<User> users = new ArrayList<>();

    static{
        users.add(new User(1,"张三","123"));
        users.add(new User(2,"马超","sb249+1"));
        users.add(new User(3,"汪晟斌","123"));
    }
    @Override
    public int add(User user) {
        System.out.println("----------------");
        users.add(user);
//        try {
//            Thread.sleep((long) (10000*Math.random()));
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println(user + "新增成功");
        System.out.println("----------------");
        return 1;
    }

    @Override
    public int update(User user) {
        System.out.println("----------------");
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getId() == user.getId()) {
                System.out.println("修改成功");
                users.set(i, user);
                return 1;
            }
        }
        System.out.println("修改失败，没有找到");
        System.out.println("----------------");
        return 0;
    }

    @Override
    public int del(int id) {
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getId() == id) {
                System.out.println("删除成功");
                users.remove(i);
                return 1;
            }
        }
        return 0;
    }

    @Override
    public User get(int id) {
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getId() == id) {
                return users.get(i);
            }
        }
        return null;
    }

    @Override
    public List<User> search() {
        return users;
    }
}
